package com.fjnu.springmvc.controller;

import com.fjnu.springmvc.vo.ActivityVO;
import com.fjnu.springmvc.vo.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.http.HttpRequest;
import java.util.*;


@Controller
@RequestMapping({"/th","/"})
@Slf4j
public class UserController {
    Map<String,String> userMap = new HashMap<>();

    @GetMapping("/")
    public String index(HttpSession session) {
        userMap.put("zhangsan","1233456");
        session.setAttribute("userMap",userMap);
        return "system/login" ;
    }
   @PostMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session, Model model) {
       Object users = session.getAttribute("userMap");
       if(userMap != null){
           userMap = (Map<String, String>) users;
       }
       if(userMap.get(username) != null && userMap.get(username).equals(password)){
           //登录成功
           session.setAttribute("user","zhangsan");
           return "redirect:/th/activity";
       } else{
           //登录失败
           model.addAttribute("error","用户名或密码错误！");
           return "system/login";
       }
    }

    @GetMapping("/register")
    public String registerView() {
        return "system/register";
    }

    @PostMapping("/regi")
    public String register(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session, Model model) {
        userMap.put(username,password);
        session.setAttribute("userMap",userMap);
        model.addAttribute("success","注册成功！");
        return "system/register";
    }

}
